/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package models

import (
	
	"github.com/astaxie/beego/orm"
	"WebAdminSys/libs"
	"errors"
	"reflect"
	"strconv"
	"fmt"
)


type UserInfo  struct {
	  ID 						int64 `orm:"pk;auto"`		//int(10) unsigned NOT NULL AUTO_INCREMENT,
	  USERID 					int	   `orm:"unique;index"`				//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
	  GAMEID 					int		`orm:"unique;index"`				//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '推广号',
	  GENDER 					byte					//smallint(2) unsigned NOT NULL DEFAULT '0' COMMENT '性别 0男 1女',
	  PREID 					string					//varchar(128) NOT NULL DEFAULT '' COMMENT '曾经用过的ID号用,号分隔',
	  FACE 						int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '头像ID',
	  AGE 						byte					//smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '年龄',
	  USERRIGHT 				int						//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户权限 0普通用户 大于为GM用户',
	  USERLEVEL 				int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户等级',
	  EXP 						int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '经验值',
	  VIPLEVEL					int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户vip等级',
	  VIPEXP 					int64	 				//unsigned NOT NULL DEFAULT '0' COMMENT '用户vip经验',
	  MOBILE 					string					//varchar(33) NOT NULL DEFAULT '' COMMENT '绑定手机号',
	  NICKNAME 					string	`orm:"index"`				//tinyblob NOT NULL COMMENT '昵称',
	  INSUREPASSWD 				string 					//varchar(33) NOT NULL DEFAULT '' COMMENT '保险箱密码',
	  DESCRIPTION 				string 					//varchar(128) NOT NULL DEFAULT '' COMMENT '个性签名',
	  INSUREGOLD 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '保险箱金币',
	  GOLD 						int64					//bigint(20) NOT NULL DEFAULT '0' COMMENT '用户金币',
	  ALLWINLOSE 				int64					//bigint(20) NOT NULL DEFAULT '0' COMMENT '用户总输赢',
	  TODAYWINLOSE 				int64					//bigint(20) NOT NULL DEFAULT '0' COMMENT '用户当日输赢',
	  TOTALRECHARGEDIAMOND 		int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '总充值钻石数',
	  TICKET 					int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '礼券',
	  DIAMOND 					int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '背包钻石数',
	  REGISTERTIME 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '注册时间',
	  REGIESTIP 				string					//varchar(33) NOT NULL DEFAULT '' COMMENT '注册IP',
	  LASTLOGINTIME 			int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '最后一次登录时间',
	  LOCATION 					string					//varchar(33) NOT NULL DEFAULT '' COMMENT '所在地区 四川省成都市武侯区',
	  VIPENDTIME 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'VIP到期时间点',
	  VIPGROWNTIME 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '每日VIP增长经验的时间点',
	  VIPACCEPTPRESENTTIME 		int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '领取每日VIP礼包时间点',
	  VIPACCEPTLV 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '已经领取过的VIP等级礼包等级',
	  TODAYVIPEXP 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '今日VIP增长经验',
	  FRESHFRIENDTIME 			int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '上一次刷新好友增长金币次数的时间',
	  ACCEPTFRIENDTIMES 		int   					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '今日已经领取好友赠送金币的次数',
	  MODIFYNICKNAMECOUNT 		int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更改昵称的次数',
	  BIRTHDAY 					string 					//varchar(33) NOT NULL DEFAULT '' COMMENT '生日',
	  LASTLOGINIP 				string 					//varchar(33) NOT NULL DEFAULT '' COMMENT '最后一次登录的IP',
	  FREEAWARDTIMES 			int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '今日免费抽奖次数',
	  NOBIGAWARDTIMES 			int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '当前累计已经多少次未中大奖的抽奖次数',
	  NOMIDDLWAWARDTIMES 		int						//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '当前累计已经多少次未中中奖的抽奖次数',
	  REFRESHRELIEFTIME 		int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '上一次刷新领取救济金的时间点',
	  GETRELIEFTIMES 			int						//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '今日已经领取救济金的次数',
	  REFRESHDAYVALUE 			int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上一次凌晨12点刷新的时间点',
	  GODWEALTHGOLD 			int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '敬财神金币',
	  LASTOFFLINETIME 			int64 					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '上一次离线时间',
	  REFRESHONLINEAWARDTIME  	int64 					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '上一次刷新领取在线礼包的时间',
	  LVSIGN 					int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '贵宾等级 0-普通用户 1-普通贵宾 2-黄金贵宾 3-钻石贵宾',
	  SPREADID 					int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '推广码',
	  TODAYSPREADAWARDCOUNT   	int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '今日已经领取推广玩家赠送奖励次数',
	  LASTSENDSPREADAWRDLV 		int 					// int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上一次给推广者添加奖励的等级',
	  LASTLASTSENDSPREADDATA  	int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上一次刷新可以领取推广者奖励的时间点',
	  CHEATRATE 				int 					//int(20) NOT NULL DEFAULT '0' COMMENT '作弊率',
	  CHEATSCORE  				int64 					//bigint(20) NOT NULL DEFAULT '0' COMMENT '作弊分数',
	  CHEATLEFTSCORE 			int64					//bigint(20) NOT NULL DEFAULT '0' COMMENT '作弊剩余分数',
	  DEVICE 					int64 					//int(20) NOT NULL DEFAULT '0' COMMENT '设备类型 1-IOS 2-android 3-windows',
	  PLATFORM 					int64 					//int(20) NOT NULL DEFAULT '0' COMMENT '平台ID,包ID',
	  STATUS 					int64 					//int(20) NOT NULL DEFAULT '0' COMMENT '账号是否锁定 0-未锁定 1-锁定',
	  ALLSELLSCORE 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '总卖分',
	  ALLBUYSCORE 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '总买分',
	  SENDNUM 					int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '赠送的序列号 具体咨询肖子涵....',
	  ACCOUNT 					string					//varchar(33) NOT NULL DEFAULT '' COMMENT '账号',
	  MACHINE 					string 					//varchar(128) NOT NULL DEFAULT '' COMMENT '机器码',
	  REVENUE 					int64					//bigint(20) NOT NULL DEFAULT '0' COMMENT '赠送的序列号 具体咨询肖子涵....',
	  VERIFYCODE   				int 					//int(10) unsigned NOT NULL DEFAULT '0' COMMENT '验证码',
	  Is_follow 				int 					//int(10) NOT NULL DEFAULT '0' COMMENT '关怀用户',
	  CHEATREASON				string 				 	//varchar(128) NOT NULL DEFAULT '' COMMENT '最后一次点控原因',
	  FIRSTBUYGOLDTIME 			int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '首次买分时间',
	  GOLDCHIPS 				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '在游戏中筹码转换成的金币(捕鱼...)',
	  CHEATENDTIME				int64					//bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '个人控制结束时间',
	  REDLOOSECHANGE			int64 					//bigint(20) NOT NULL DEFAULT '0' COMMENT '红包零钱',
}


type SuperVipInfo struct{
	  ID 							int64 `orm:"pk;auto"`
	  UserID 						int64  `orm:"unique"`	//int(11) NOT NULL COMMENT '用户ID',
	  Guarantee 					string 				//varchar(500) NOT NULL COMMENT '说明',
	  LVSIGN 						int 				//int(11) NOT NULL COMMENT '等级',
	  CreateTime 					string 				//date NOT NULL COMMENT '创建时间',
	  sType 						int 				//int(11) NOT NULL,
}
type DayPlathFormInfo struct{
	  ID 							int64 `orm:"pk;auto"`
	  DataDateTime 					int64  `orm:"unique"`	//0当天0点时间
	  RegCount 						int64 
	  LogonCount 					int64 
	  RMB							int64
	  TopOnLine 					int64 
	  GMAddGold 					int64 
	  SysGold 						int64 
	  UserGold 						int64 
	  VIPGold 						int64 
	  SellScore 					int64 
	  SellCount 					int64 
	  SysWinLose 					int64 
	  BloodScore 					int64 
	  BuyScore		 				int64 
	  BuyCount	 					int64 
	  UserCount	 					int64 

}
func ExtremVipUserInfo(usercondition,userconditioninfo,duserclasscondition string) ([]orm.Params, int64,error) {
	
	o := orm.NewOrm(); 
	var maps []orm.Params  
    strsql := "SELECT S.UserID,GameID,cast(NickName as char) as NickName,cast(DESCRIPTION as char) as DESCRIPTION,S.LVSIGN,S.Gold,S.InsureGold,"
    strsql += "S.Gold+S.InsureGold AS zichan, IFNULL(B.SellScore,0)  AS SellScore, IFNULL(C.SellCount,0)  AS SellCount,Guarantee,S.DIAMOND,"
    strsql += "IFNULL(BB.CreateTime,NOW()) AS CreateTime FROM UserInfo AS S  LEFT JOIN SuperVipInfo AS BB ON BB.USERID=S.USERID LEFT "
    strsql += "JOIN (SELECT IFNULL(SUM(SellScore), 0) AS SellScore,USERID FROM  UserGiveGiftSub WHERE  UpdateTime>UNIX_TIMESTAMP(date_sub(curdate(),"
    strsql += "interval 7 day)) AND  UpdateTime <UNIX_TIMESTAMP(NOW()) GROUP BY USERID) AS B ON S.USERID=B.USERID LEFT JOIN (SELECT IFNULL(SUM(SellCount), "
    strsql += "0) AS SellCount,USERID FROM  UserGiveGiftSub GROUP BY USERID) AS C ON S.USERID=C.USERID WHERE  S.LVSIGN IN (1,2,3)"
	
	if usercondition == "0" && userconditioninfo != "" {
		strsql = fmt.Sprintf("%s AND GameID = %s",strsql,userconditioninfo);
	}
	//登录名
	if usercondition == "1" && userconditioninfo != "" {
		
		strsql = fmt.Sprintf("%s AND ACCOUNT = '%s'",strsql,userconditioninfo);
	}
	//昵称
	if usercondition == "2" && userconditioninfo != "" {
		
		strsql = fmt.Sprintf("%s AND NICKNAME = '%s'",strsql,userconditioninfo);
	}
		//UserID
	if usercondition == "3" && userconditioninfo != "" {
		strsql = fmt.Sprintf("%s AND USERID = %s",strsql,userconditioninfo);	
	}
	if duserclasscondition == "1" || duserclasscondition == "2" {
		strsql = fmt.Sprintf("%s AND S.LVSIGN = %s",strsql,duserclasscondition);
	} 
	strsql +=" ORDER BY S.LVSIGN DESC,SellScore DESC,GameID";
	total,err := o.Raw(strsql).Values(&maps);
	fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps, total,err
}
func ExtremVipUserInfoByUserID(UserID int64, fildnames ...string) ( []orm.Params){
	var maps []orm.Params
	query := orm.NewOrm().QueryTable(TableName("UserInfo"))
	
	query = query.Filter("USERID", UserID)

	query.Limit(1).Values(&maps,fildnames...);

	return maps
}
func ExtremVipUserCreateTime( UserID int64) (string,string){

	query := orm.NewOrm().QueryTable(TableName("SuperVipInfo"))
	query = query.Filter("UserID", UserID)
	query = query.Filter("LVSIGN__in", 1,2,3)
	var data SuperVipInfo;
	query.One(&data, "CreateTime", "Guarantee");
	return data.CreateTime,data.Guarantee;
}



func ExtremVipUserAllGold( usercondition,userconditioninfo,duserclasscondition string) (string) {
	
	o := orm.NewOrm();
	var maps []orm.Params
	var UserScore string = "0";
	Lvsigh,lverr := strconv.Atoi(duserclasscondition);
	tmpuserconditioninfo := "%"+userconditioninfo+"%";
	if(usercondition == "0" && userconditioninfo != ""){
		UserID,_ := strconv.Atoi(userconditioninfo);
		if(Lvsigh > 0 && lverr == nil ){

			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE USERID =  ? AND LVSIGN > 0 AND LVSIGN = ?",UserID,Lvsigh).Values(&maps);
		}else{
		
			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE USERID =  ? AND LVSIGN > 0",UserID).Values(&maps);
		}
	}else if(usercondition == "1" && userconditioninfo != ""){

		if(Lvsigh > 0 && lverr == nil ){

			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE ACCOUNT LIKE ? AND LVSIGN > 0 AND LVSIGN = ?",tmpuserconditioninfo,Lvsigh).Values(&maps);
		}else{
		
			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE ACCOUNT LIKE ? AND LVSIGN > 0;",tmpuserconditioninfo).Values(&maps);
		}
	}else if(usercondition == "2" && userconditioninfo != ""){	
		if(Lvsigh > 0 && lverr == nil ){

			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE NICKNAME LIKE ? AND LVSIGN > 0 AND LVSIGN = ?",tmpuserconditioninfo,Lvsigh).Values(&maps);
		}else{
		
			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE NICKNAME LIKE ? AND LVSIGN > 0;",tmpuserconditioninfo).Values(&maps);
		}
	}else if(usercondition == "2" && userconditioninfo != ""){
		UserID,_ := strconv.Atoi(userconditioninfo);
		if(Lvsigh > 0 && lverr == nil ){

			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE USERID =  ? AND LVSIGN > 0 AND LVSIGN = ?",UserID,Lvsigh).Values(&maps);
		}else{
		
			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE USERID =  ? AND LVSIGN > 0",UserID).Values(&maps);
		}
	}else{
		if(Lvsigh > 0 && lverr == nil ){

			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE LVSIGN > ? AND LVSIGN = ?",0,Lvsigh).Values(&maps);
		}else{
		
			o.Raw("SELECT SUM(Gold)+SUM(INSUREGOLD) FROM UserInfo WHERE LVSIGN > 0").Values(&maps);
			//fmt.Println("===========maps = fffffffffffffff" ,);
		}
	}

	if(len(maps) >= 1 && reflect.TypeOf(maps[0]["SUM(Gold)+SUM(INSUREGOLD)"]) != nil ){
		UserScore = maps[0]["SUM(Gold)+SUM(INSUREGOLD)"].(string)
	}
	//fmt.Println("===========maps =" ,len(maps),maps);
	return UserScore;
}

func ExtremVipUserCountSendGold()(string,string) {
	
	o := orm.NewOrm();
	var maps []orm.Params
	var SellCount string = "0";
	var SellScore string ="0";
	

	o.Raw("SELECT IFNULL(SUM(SellScore),0) AS SellScore,IFNULL(SUM(SellCount),0) AS SellCount FROM UserGiveGiftSub WHERE UpdateTime=UNIX_TIMESTAMP(LEFT(NOW(),10)) AND UserID IN (SELECT UserID FROM UserInfo WHERE  LVSIGN=1 )").Values(&maps);
	

	if(len(maps) >= 1 && reflect.TypeOf(maps[0]["SellCount"]) != nil ){
		SellCount = maps[0]["SellCount"].(string)
	}
	if(len(maps) >= 1 && reflect.TypeOf(maps[0]["SellScore"]) != nil ){
		SellScore = maps[0]["SellScore"].(string)
	}
	//fmt.Println("?????????????????==maps =" ,len(maps),maps,"-*-*-*-*",reflect.TypeOf(maps[0]["SellScore"]));
	return SellCount,SellScore;;
}

//每日注册量 各平台注册量
func RegisterCountDaily(date1,date2 int64,platform int )( *[]orm.Params,int){

	o := orm.NewOrm();
	sql := "";
	sql = sql + " select FROM_UNIXTIME(REGISTERTIME,'%Y-%m-%d')as time1,"
	sql = sql + " DEVICE as device1,COUNT(ID) as count1  FROM UserInfo "
	sql = sql + " WHERE "
	if platform != 0 {
		
		sql = sql + " DEVICE = "+ strconv.FormatInt(int64(platform),10) + " and "
	}
	sql = sql + " REGISTERTIME BETWEEN " + strconv.FormatInt(date1,10) +" and "+strconv.FormatInt(date2,10) 
	sql = sql + " GROUP BY DEVICE,time1 ORDER BY REGISTERTIME;"
	var maps []orm.Params
	o.Raw(sql).Values(&maps);
	
	fmt.Println("###",sql)
	return &maps,len(maps)

}

//日登录量统计
func LoginCountDaily(page, pageSize int,date1,date2 int64,platform int )( *[]orm.Params,int64){

		dstr1 := strconv.FormatInt(date1,10);
		dstr2 := strconv.FormatInt(date2,10);
		pfm  := strconv.FormatInt(int64(platform),10);

		o := orm.NewOrm();
		sql := " DROP TABLE  IF EXISTS xxx; ";
		sql = sql + " create temporary table xxx "
		sql = sql + " SELECT regTime,loginTime,count(id) as newLoginCount  FROM "
		sql = sql + " (SELECT ID, FROM_UNIXTIME(REGISTERTIME,'%Y-%m-%d')as regTime , FROM_UNIXTIME(LASTLOGINTIME,'%Y-%m-%d')as loginTime FROM UserInfo "
		sql = sql + " WHERE REGISTERTIME  BETWEEN " +  dstr1 + " and " + dstr2 
		if platform != 0 { sql = sql + " and  DEVICE = "+ pfm }
		sql = sql + " ) ss "
		sql = sql + " WHERE regTime = loginTime GROUP BY loginTime; "

		sql = sql + " DROP TABLE  IF EXISTS ooo;"
		sql = sql + " create temporary table ooo  "
		sql = sql + " SELECT FROM_UNIXTIME(LASTLOGINTIME,'%Y-%m-%d')as loginTime ,count(id) as loginCount FROM UserInfo "
		sql = sql + " WHERE LASTLOGINTIME  BETWEEN " +  dstr1 + " and " + dstr2 
		if platform != 0 { sql = sql + " and  DEVICE = "+ pfm }
		sql = sql + "  GROUP BY logintime; "

		
		


		sql = sql + " SELECT ooo.logintime,ooo.logincount,IFNULL(xxx.newlogincount,0) as newlogincount FROM ooo "
		sql = sql + " LEFT JOIN xxx on ooo.loginTime = xxx.loginTime "
		sql = sql + " LIMIT 0,30 ; "
		sql = sql + " DROP TABLE  IF EXISTS ooo; "
		sql = sql + " DROP TABLE  IF EXISTS xxx; "
		fmt.Println("sql语句:");

	
		sql = "CALL WB_Userinfo_LoginCountDaily(?,?,?,?,?,@OutTotal)"

		fmt.Println(sql)
		
		var maps []orm.Params

		o.Raw(sql,date1,date2,platform,(page-1)*pageSize,pageSize).Values(&maps);
		
		//取传出参数
		sql = "SELECT @OutTotal as total"
		var totalMap []orm.Params;
		o.Raw(sql).Values(&totalMap)
		var tt = (totalMap[0]["total"]);
		var total int64 = 0;
		
		if tt != nil {
			 total,_ = strconv.ParseInt(tt.(string), 10, 64)
		}
		

		fmt.Println(total)
		return &maps,total;
	}

	//活跃统计
func ActiveCount(page, pageSize int,date1,date2 int64,platform ,activeType int)( *[]orm.Params,int64){
			sql :="";
			if activeType ==1{
				sql = "CALL WB_Userinfo_ActiveCountDAU(?,?,?,?,?,?,@OutTotal)"
			}else{
				sql = "CALL WB_Userinfo_ActiveCountWAU(?,?,?,?,?,?,@OutTotal)"
			}
			
			fmt.Println(sql)
			if page <1 {
				page = 1;
			}
			var maps []orm.Params
			o := orm.NewOrm();
			o.Raw(sql,date1,date2,platform,(page-1)*pageSize,pageSize,activeType).Values(&maps);
			




			//取传出参数
			sql = "SELECT @OutTotal as total"
			var totalMap []orm.Params;
			o.Raw(sql).Values(&totalMap)
			var tt = (totalMap[0]["total"]);
			var  total int64 = 0;
			if tt != nil{
				 total,_ = strconv.ParseInt(tt.(string), 10, 64)
			}
		
	
			fmt.Println(total)
			return &maps,total;
}

	//获取用户留存统计
	func UserRetentionCount(page, pageSize int,date1,date2 int64,platform  int)( *[]orm.Params,int64){
		sql :="";
	
		sql = "CALL WB_Userinfo_RetentionCount(?,?,?,?,?,?,@OutTotal)"
		
		fmt.Println(sql)
		
		var maps []orm.Params
		o := orm.NewOrm();
		if page <1 {
			page = 1;
		}
		o.Raw(sql,date1,date2,platform,(page-1)*pageSize,pageSize,1).Values(&maps);
		


		//取传出参数
		sql = "SELECT @OutTotal as total"
		var totalMap []orm.Params;
		o.Raw(sql).Values(&totalMap)
		var tt = (totalMap[0]["total"]);
		var total int64 = 0;
		if tt !=nil {
			total,_ = strconv.ParseInt(tt.(string), 10, 64)
		}
		
	
		return &maps,total;
}

/******************************************************************************************
//平台明细 数据库处理
******************************************************************************************/

func ExtremVipPlatformTodayDetail()(*[]orm.Params){
	o := orm.NewOrm(); 
	var maps []orm.Params;
	sql := "CALL WB_PlatformTodayDetail()"
	o.Raw(sql).Values(&maps);
	
	//fmt.Println(&maps);


	return &maps;
}


func (data *SuperVipInfo) ModefySuperVipInfo() error{
	o := orm.NewOrm();

	exist := o.QueryTable("SuperVipInfo").Filter("UserID",data.UserID).Exist();
	if !exist {
		data.CreateTime = libs.GetDateNowString();
		_,err := o.Insert(data)
		return err;
	}else{
		if data.Guarantee != ""{
			tmpData := orm.Params{
	   				 "Guarantee":data.Guarantee,
	   				  "LVSIGN": data.LVSIGN,
					}
			_,err := o.QueryTable("SuperVipInfo").Filter("UserID",data.UserID).Update(tmpData)
			return err;	
		}else{
				tmpData := orm.Params{
	   				  "LVSIGN": data.LVSIGN,
					}
			_,err := o.QueryTable("SuperVipInfo").Filter("UserID",data.UserID).Update(tmpData)
			return err;	
		}

	}
	
	return nil;
}
func (data *SuperVipInfo) ModefySuperVipUserID(newUserID int64) error{
	o := orm.NewOrm();
	exist := o.QueryTable("SuperVipInfo").Filter("UserID",data.UserID).Exist();
	if !exist && data.LVSIGN <= 0{
		return nil;
	}
	if !exist {
		data.UserID = newUserID;
		data.CreateTime = libs.GetDateNowString();
		_,err := o.Insert(data)
		return err;
	}else{
			
		tmpData := orm.Params{
			"UserID":newUserID,
	   		"LVSIGN": data.LVSIGN,
			}
		_,err := o.QueryTable("SuperVipInfo").Filter("UserID",data.UserID).Update(tmpData)
		return err;	
	}
	return nil;
}

func (data *UserInfo) ModefyUserInfoUserID(oldUSerID,newUserID int) (error){
	o := orm.NewOrm();
	exist := o.QueryTable("UserInfo").Filter("USERID",newUserID).Exist();
	if exist {
		return errors.New("新帐号在用户表中存在！")
	}
	exist = o.QueryTable("UserInfo").Filter("USERID",oldUSerID).Exist();
	if !exist {
		return errors.New("老帐号在用户表中不存在！")
	}

	strsql := fmt.Sprintf("UPDATE UserInfo SET USERID = %d,GAMEID = %d WHERE USERID = %d",newUserID,newUserID,oldUSerID);	
	_,err := o.Raw(strsql).Exec();
	
	if err != nil{
		return err;
	}
	
	return err

}
func GetUserUserInfoSigin(newUserID int)  (error,int){
	o := orm.NewOrm();
	var maps []orm.Params

	strsql := fmt.Sprintf("SELECT LVSIGN FROM UserInfo WHERE USERID = %d",newUserID);	
	_,err := o.Raw(strsql).Values(&maps);
	if err == nil{
		if( len(maps) >= 1){
			if reflect.TypeOf(maps[0]["LVSIGN"]) != nil{
				strTmp := maps[0]["LVSIGN"].(string);
				Sing,err := strconv.Atoi(strTmp);
				return err,Sing;

			}else{
				return errors.New("新帐号查询等级失败.！"),0
			}
		}else{
			return errors.New("新帐号查询等级失败！"),0
		}
	}

	return err,0

}

func (data *UserInfo) ModefyUserInfoFollow() error{
	o := orm.NewOrm();
	strsql := fmt.Sprintf("UPDATE UserInfo SET Is_follow = %d WHERE USERID = %d",data.Is_follow,data.USERID);	
	_,err := o.Raw(strsql).Exec();
	return err	

}
func ExtremVipSendGitfListTable(page, pageSize int,usercondition,userconditioninfo,strTime1,strtime2 string) ([]orm.Params,int64,error) {
	
	o := orm.NewOrm(); 
	var maps []orm.Params  
    strsql := "SELECT A.UserID,GameID,cast(NickName as char) as NickName,IFNULL(SUM(SellCount),0) AS SellCount,IFNULL(SUM(SellScore),0) AS SellScore,IFNULL(SUM(BuyScore),0) AS BuyScore,IFNULL(SUM(SellScore)-SUM(BuyScore),0) AS ZSSC,IFNULL(C.C,0) AS C,Guarantee,IFNULL(B.CreateTime,NOW()) AS CreateTime FROM  UserGiveGiftSub AS U LEFT JOIN UserInfo AS A  ON U.UserID=A.UserID  LEFT JOIN SuperVipInfo AS B ON A.USERID=B.USERID LEFT JOIN ";
    strsql +=  " (SELECT COUNT(DISTINCT UserID) AS C,Vip_userid FROM UserGiveGiftSub WHERE 1=1"

	if   strTime1 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime >= %d ",strsql,libs.GetStringTimeToUnix(strTime1,"Y-m-d"));
	}
	if  strtime2 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime <= %d ",strsql,libs.GetStringTimeToUnix(strtime2,"Y-m-d"));
	}
    strsql += " GROUP BY Vip_userid) AS C ON C.Vip_userid=A.UserID WHERE A.LVSIGN IN (1,2,3) "
   
   	//推广号
   	if usercondition == "0" && userconditioninfo != "" {
		strsql = fmt.Sprintf("%s AND GameID = %s",strsql,userconditioninfo);
	}
	//登录名
	if usercondition == "1" && userconditioninfo != "" {
		
		strsql = fmt.Sprintf("%s AND ACCOUNT = '%s'",strsql,userconditioninfo);
	}
	//昵称
	if usercondition == "2" && userconditioninfo != "" {
		
		strsql = fmt.Sprintf("%s AND NICKNAME = '%s'",strsql,userconditioninfo);
	}
	//UserID
	if usercondition == "3" && userconditioninfo != "" {
		strsql = fmt.Sprintf("%s AND USERID = %s",strsql,userconditioninfo);	
	} 

	if  strTime1 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime >= %d ",strsql,libs.GetStringTimeToUnix(strTime1,"Y-m-d"));
	}
	if  strtime2 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime <= %d ",strsql,libs.GetStringTimeToUnix(strtime2,"Y-m-d"));
	}

	strsql += " GROUP BY GameID,NickName,A.UserID,C.C ORDER BY SUM(SellScore) DESC "; 
	
	offset := (page - 1) * pageSize
 	strsql = fmt.Sprintf(" %s LIMIT %d,%d;",strsql,offset,pageSize);

	
	total,err := o.Raw(strsql).Values(&maps);
	fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps,total,err

}
func ExtremVipSendGitfListTableTitle(usercondition,userconditioninfo,strTime1,strtime2 string) ([]orm.Params,int64,error) {
	
	o := orm.NewOrm(); 
	var maps []orm.Params  
	strsql := "SELECT SUM(UTITLE.SellCount) AS SC,SUM(UTITLE.SellScore) AS SS,SUM(UTITLE.ZSSC) AS TZSSC,SUM(UTITLE.BuyScore) AS BC FROM ("
    strsql += " SELECT IFNULL(SUM(SellCount),0) AS SellCount,IFNULL(SUM(SellScore),0) AS SellScore,IFNULL(SUM(BuyScore),0) AS BuyScore,IFNULL(SUM(SellScore)-SUM(BuyScore),0) AS ZSSC " 
    strsql += " FROM  UserGiveGiftSub AS U LEFT JOIN UserInfo AS A  ON U.UserID=A.UserID  LEFT JOIN SuperVipInfo AS B ON A.USERID=B.USERID LEFT JOIN ";
    strsql += " (SELECT COUNT(DISTINCT UserID) AS C,Vip_userid FROM UserGiveGiftSub WHERE 1=1"

	if   strTime1 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime >= %d ",strsql,libs.GetStringTimeToUnix(strTime1,"Y-m-d"));
	}
	if  strtime2 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime <= %d ",strsql,libs.GetStringTimeToUnix(strtime2,"Y-m-d"));
	}
    strsql += " GROUP BY Vip_userid) AS C ON C.Vip_userid=A.UserID WHERE A.LVSIGN IN (1,2,3) "
   
   	//推广号
   	if usercondition == "0" && userconditioninfo != "" {
		strsql = fmt.Sprintf("%s AND GameID = %s",strsql,userconditioninfo);
	}
	//登录名
	if usercondition == "1" && userconditioninfo != "" {
		
		strsql = fmt.Sprintf("%s AND ACCOUNT = '%s'",strsql,userconditioninfo);
	}
	//昵称
	if usercondition == "2" && userconditioninfo != "" {
		
		strsql = fmt.Sprintf("%s AND NICKNAME = '%s'",strsql,userconditioninfo);
	}
	//UserID
	if usercondition == "3" && userconditioninfo != "" {
		strsql = fmt.Sprintf("%s AND USERID = %s",strsql,userconditioninfo);	
	} 

	if  strTime1 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime >= %d ",strsql,libs.GetStringTimeToUnix(strTime1,"Y-m-d"));
	}
	if  strtime2 != "" {
		strsql = fmt.Sprintf("%s  AND UpdateTime <= %d ",strsql,libs.GetStringTimeToUnix(strtime2,"Y-m-d"));
	}

	strsql += " ) AS UTITLE "; 
 	

	
	total,err := o.Raw(strsql).Values(&maps);
	//fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps,total,err
}

func ExtremVipGetRegCount( BeforTime,EndTime int64)(int64){

	query := orm.NewOrm().QueryTable(TableName("UserInfo"))
	query = query.Filter("REGISTERTIME__gte", BeforTime)
	query = query.Filter("REGISTERTIME__lte", EndTime)
	UserRegtotal, _ := query.Count()
	
	return UserRegtotal;
}
func ExtremVipGetLoginLobbyCount(BeforTime,EndTime int64)(int64) {

	o := orm.NewOrm(); 
	var maps []orm.Params  
	strsql := "SELECT Count(1) AS total FROM UserEnterRecord  WHERE E_Type = 0 AND ServerID = 0 "
	if BeforTime != 0 {
		strsql = fmt.Sprintf("%s  AND EnterTime >= %d",strsql,BeforTime);
	}
	if EndTime != 0 {
		strsql = fmt.Sprintf("%s  AND EnterTime <= %d",strsql,EndTime);
	}
	o.Raw(strsql).Values(&maps);

	var UserLogintotal int64 = 0;
	if len(maps) > 0{
		UserLogintotal,_= strconv.ParseInt(maps[0]["total"].(string),10,64);
		
	}
	return UserLogintotal
}
func ExtremVipGetUserCharge(BeforTime,EndTime int64)(int64) {
	
	
	tmpsql := " SELECT IFNULL(SUM(RMB),0) AS TRMB FROM UserPurchaseInfo "
	tmpsql = fmt.Sprintf(" %s WHERE PURCHASETIME >= %d AND PURCHASETIME <= %d" ,tmpsql,BeforTime,EndTime);
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	var total int64 = 0;
	if err != nil{
		return total
	}
	if(len(maps) > 0){
		total,_ = strconv.ParseInt(maps[0]["TRMB"].(string), 10, 64)
	}
	
	return total
	
}

func ExtremVipGetUserOnline()(int64) {
	
	query := orm.NewOrm().QueryTable(TableName("UserOnlineInfo"))

	UserOnlineCount, _ := query.Count()
	return UserOnlineCount;
	
}

func ExtremGMAddGoldS(BeforTime,EndTime int64)(int64) {
	
	
	tmpsql := " SELECT SUM(Amount) AS TAmount FROM GMAddRecord "
	tmpsql = fmt.Sprintf(" %s WHERE Unix_time >= %d AND Unix_time <= %d AND Gold_type = 1" ,tmpsql,BeforTime,EndTime);
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	var total int64 = 0;
	if err != nil{
		return total
	}
	if(len(maps) > 0){
		total,_ = strconv.ParseInt(maps[0]["TAmount"].(string), 10, 64)
	}
	
	return total
	
}
func ExtremGetSysGold() (int64){
	tmpsql := " SELECT SUM(GOLD + INSUREGOLD) AS TGolds FROM UserInfo WHERE `STATUS` = 0 "
	
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	var total int64 = 0;
	if err != nil{
		return total
	}
	if(len(maps) > 0){
		total,_ = strconv.ParseInt(maps[0]["TGolds"].(string), 10, 64)
	}
	
	return total
}

func ExtremGetUserGold() (int64){
	tmpsql := " SELECT SUM(GOLD + INSUREGOLD) AS TGolds FROM UserInfo  WHERE LVSIGN = 0 AND `STATUS` = 0";
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	var total int64 = 0;
	if err != nil{
		return total
	}
	if(len(maps) > 0){
		total,_ = strconv.ParseInt(maps[0]["TGolds"].(string), 10, 64)
	}
	
	return total
}
func ExtremGetVIPUserGold() (int64){
	tmpsql := " SELECT SUM(GOLD + INSUREGOLD) AS TGolds FROM UserInfo  WHERE LVSIGN >= 1 AND LVSIGN <= 3 AND `STATUS` = 0";
	var maps []orm.Params
	o := orm.NewOrm(); 
	_,err := o.Raw(tmpsql).Values(&maps);

	var total int64 = 0;
	if err != nil{
		return total
	}
	if(len(maps) > 0){
		total,_ = strconv.ParseInt(maps[0]["TGolds"].(string), 10, 64)
	}
	
	return total
}

func ExtremVipSendGitfListData(BeforTime,EndTime int64) ([]orm.Params,int64,error) {
	
	o := orm.NewOrm(); 
	var maps []orm.Params  
	strsql := "SELECT IFNULL(SUM(SellScore),0) AS SellScore,IFNULL(SUM(SellCount),0) AS SellCount,IFNULL(SUM(BuyScore),0) AS BuyScore,IFNULL(SUM(BuyCount),0) AS BuyCount,COUNT(1) AS UserCount FROM UserGiveGiftSub WHERE 1= 1 "


	if   BeforTime != 0 {
		strsql = fmt.Sprintf("%s  UpdateTime >= %d  ",strsql,BeforTime);
	}
	if  EndTime != 0 {
		strsql = fmt.Sprintf("%s  AND UpdateTime < %d ",strsql,EndTime);
	}
    strsql += " AND Vip_userid IN (SELECT UserID FROM UserInfo WHERE  LVSIGN >= 1 ) "

 	

	
	total,err := o.Raw(strsql).Values(&maps);
	//fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps,total,err
}
func ExtremVipSendGitfListNum(BeforTime,EndTime int64) (int64,int64,int64,int64,int64){

	Datas,_,_ := ExtremVipSendGitfListData(BeforTime,EndTime);
	var SellScore, SellCount,BuyScore,BuyCount,UserCount int64 = 0,0,0,0,0;
	if len(Datas) > 0{
		SellScore,_= strconv.ParseInt(Datas[0]["SellScore"].(string),10,64);
		SellCount,_= strconv.ParseInt(Datas[0]["SellCount"].(string),10,64);
		BuyScore,_ = strconv.ParseInt(Datas[0]["BuyScore"].(string),10,64);
		BuyCount,_ = strconv.ParseInt(Datas[0]["BuyCount"].(string),10,64);
		UserCount,_= strconv.ParseInt(Datas[0]["UserCount"].(string),10,64);	
	}
	return SellScore, SellCount,BuyScore,BuyCount,UserCount
}

func ExtremVipGameBooldData() (int64){

	o := orm.NewOrm(); 
	var maps []orm.Params  
	strsql := "SELECT IFNULL(SUM(RoomBloodPool),0) AS AllBoolds FROM RoomCheatMain "
	o.Raw(strsql).Values(&maps);
	var AllBoolds int64 = 0;
	if len(maps) > 0{
		AllBoolds,_= strconv.ParseInt(maps[0]["AllBoolds"].(string),10,64);
		
	}
	return AllBoolds
}
func ExtremSysWinLoseData(BeforTime int64) (int64){

	o := orm.NewOrm(); 
	var maps []orm.Params  
	strsql := "SELECT IFNULL(SUM(TodayWinLose),0) AS SysWinLose FROM RoomCheatSub  "
	strsql = fmt.Sprintf("%s WHERE UpdateTime = %d ",strsql,BeforTime);
	o.Raw(strsql).Values(&maps);
	var SysWinLose int64 = 0;
	if len(maps) > 0{
		SysWinLose,_= strconv.ParseInt(maps[0]["SysWinLose"].(string),10,64);
	}
	return SysWinLose
}


func PlatFromDeTailTData(page, pageSize int, strsql string) ([]orm.Params, int64,error) {
	offset := (page - 1) * pageSize
	o := orm.NewOrm(); 
	var maps []orm.Params  
 
	tmpsql := "SELECT FROM_UNIXTIME(DataDateTime) AS DataDateTime,RegCount,LogonCount,RMB,TopOnLine,GMAddGold,SysGold,UserGold,VIPGold,SellScore,SellCount,SysWinLose,BloodScore,BuyScore,BuyCount ,UserCount";
	tmpsql += " FROM DayPlathFormInfo  WHERE 1 = 1  ";
	
	tmpsql = fmt.Sprintf("%s  %s",tmpsql,strsql);

	tmpsql += " ORDER BY DataDateTime DESC"

	tmpsql = fmt.Sprintf(" %s LIMIT %d,%d;",tmpsql,offset,pageSize);
	tmpSql2 := "SELECT COUNT(*) AS total  FROM DayPlathFormInfo  WHERE 1=1 ";

	tmpSql2 = fmt.Sprintf("%s  %s",tmpSql2,strsql);
	 
	_,err := o.Raw(tmpsql).Values(&maps);
	var maps2 []orm.Params  
	total,err := o.Raw(tmpSql2).Values(&maps2);
	if len(maps2) >= 1{
		strtmp :=  maps2[0]["total"].(string);
		total,_ = strconv.ParseInt(strtmp, 10, 64)  
	}
	//fmt.Println("===========222222222",maps2, "maps =" ,len(maps2));
	//fmt.Println("===========",maps, "maps =" ,len(maps));
	return maps, total,err
}

func PlatFromDeTailTDataTitle( strsql string) ([]orm.Params, int64,error) {
	
	o := orm.NewOrm(); 
	var maps []orm.Params  
 
	tmpsql := "SELECT SUM(RMB) AS TRMB,SUM(GMAddGold) AS TGMGD,SUM(SellScore) AS TSS,SUM(SysWinLose) AS TSWL,SUM(BuyScore) AS TBS ";
	tmpsql += " FROM DayPlathFormInfo  WHERE 1 = 1  ";
	
	tmpsql = fmt.Sprintf(" %s %s",tmpsql,strsql);

	total,err := o.Raw(tmpsql).Values(&maps);

	return maps, total,err
}